package com.seo.controller.task;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.task.impl.UserKeywordsTaskServiceImpl;
import com.seo.utils.CommonUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户相关controller
 * 
 * @author Administrator
 * @version 2018.08.02 17:40
 */
@Slf4j
@Controller
@RequestMapping(value = "/task/keywords")
public class UserKeywordsController {

	@Autowired
	private UserKeywordsTaskServiceImpl userKeywordsTaskService;

	@Autowired
	private PageTool pageTool;

	@RequestMapping
	public String list(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpSession session) {
		log.info("进入关键词扣费调度列表...");

		PageInfo<?> page = pageTool.getAutoPage(userKeywordsTaskService::getTask, params);
		request.setAttribute("page", page);

		return "task/keywords/list";

	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		log.info("进入添加关键词扣费调度页面");

		request.setAttribute("action", "addKeywords");
		return "task/keywords/edit";
	}

	@RequestMapping("/addKeywords")
	@ResponseBody
	public ReturnBody addKeywords(String taskTime) {
		ReturnBody rb = ReturnBody.init();
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-添加关键词扣费调度", tradeId);
		rb.run(userKeywordsTaskService.runUserKeywordsDeduc(tradeId, taskTime, true));
		return rb;
	}

	@RequestMapping("/runKeywords")
	@ResponseBody
	public ReturnBody runKeywords(String taskTime) {
		ReturnBody rb = ReturnBody.init();
		String tradeId = CommonUtil.getUUID();
		log.info("添加关键词扣费调度");
		rb.run(userKeywordsTaskService.runUserKeywordsDeduc(tradeId, taskTime, false));
		return rb;
	}

}
